{% if new %}
    <div class="grp-group grp-tabular" id="question_{{ new }}">
{% endif %}
<form enctype="multipart/form-data" action="" method="post" id="question_form_{{ question_form.prefix }}">
    {{ question_form.management_form }}
    {{ answers_formset.management_form }}
    {{ question_form.errors }}
        <div class="grp-module grp-group grp-tabular">
            <h2 class="gpr-collapse-handler question_header">Question {{ question_form.order }} </h2>
            {{ question_form.non_field_errors }}
          
            {% for hidden in question_form.hidden_fields %} {{ hidden }} {% endfor %}
            <table>
            <tr>
              <td style="text-align: center;">
                <div>
                     {{ question_form.group.label }} {{ question_form.group.errors }} {{ question_form.group }}
                      <a href="/admin/omr/questiongroup/add/" class="add-another" id="add_id_{{question_form.group.html_name}}" onclick="return showAddAnotherPopup(this);"><img src="/static/grappelli/img/admin/icon_addlink.gif" width="10" height="10" alt="Add Another"></a>
                      &nbsp;&nbsp;&nbsp;&nbsp;
                      {{ question_form.type.label }} {{ question_form.type.errors }} {{ question_form.type }} &nbsp;&nbsp;&nbsp;&nbsp;
                      {{ question_form.point_value.label }} {{ question_form.point_value.errors }} {{ question_form.point_value }} &nbsp;&nbsp;&nbsp;&nbsp;
                  <br/>
                      {{ question_form.question.errors }} {{ question_form.question }}
                      <div id="question_{% if new %}{{ new }}{% else %}{{ question.id }}{% endif %}_is_true_div" style="{% if question.type != "True/False" %}display: none;{% endif %}"> Answer: {{ question_form.is_true }} {{ question_form.is_true.errors }} </div>
              
                </div>
              </td>
              
              <td>
                <div>
                    {{ question_form.benchmarks.label }}
                    <a onclick="return showBenchmarkPopup(this, {% if new %}'{{ new }}'{% else %}{{ question.id }}{% endif %});" href="/omr/benchmark">View Benchmarks</a>
                    {{ question_form.benchmarks.errors }} <br/> {{ question_form.benchmarks }}
                </div>
                <div>
                    {{ question_form.themes.label }} {{ question_form.themes.errors }}
                    <a href="/admin/omr/theme/add/" class="add-another" id="add_id_{{question_form.themes.html_name}}" onclick="return showAddAnotherPopup(this);"><img src="/static/grappelli/img/admin/icon_addlink.gif" width="10" height="10" alt="Add Another"></a>
                </div>
                {{ question_form.themes }}
                {% if new %}
                    <div>
                    {{ question_form.save_to_bank.label }} {{ question_form.save_to_bank }}
                    </div>
                {% endif %}
              </td>
            </tr>
          </table>
        </div>
        <div class="grp-module" id="question_{% if new %}{{ new }}{% else %}{{ question.id }}{% endif %}_answers_div" style="{% if question.type == "True/False" %}display: none;{% endif %}>
            <h2 class="collapse-handler">Answers</h2>
            <div id="answers_div_{{ question_form.prefix }}">
              {% for answer in answers_formset %}
                  <div class="row module answer_div answer_div_{{ question_form.prefix }}">
                    {% include "omr/answer_form.html" with answer=answer finalized=finalized %}
                  </div>
              {% endfor %}
            </div>
        </div>
        
    {% if not finalized %}    
        <a href="javascript:;" id="add_more_{{ question_form.prefix }}">Add Another Answer</a><br/>
    {% endif %}
    <div class="module save_bar">
        {% if not new and not finalized %}
            <a onclick="delete_question({{ question.id }}); return false;" class="grp-button grp-delete-link">Delete</a>
        {% endif %}
        
        <span style="float: right;">
            <a id="save_{{ question_form.prefix }}" class="grp-button" onclick="return false;">Save</a>
            <script>
                $('#save_{{ question_form.prefix }}').click(function() {
                  {% if new %}
                    save_question("question_form_{{ question_form.prefix }}", '{{ new }}');
                  {% else %}
                    save_question("question_form_{{ question_form.prefix }}", {{ question.id }});
                  {% endif %}
                });
            </script>
        </span>
        <a
           class="grp-button"
           onclick="cancel_edit({% if new %}'{{ new }}'{% else %}{{ question.id }}{% endif %}); return false;">
            Cancel, discard changes
        </a>
    </div>
    
    <script>
      var form_count_{{ question_form.prefix }} = {{ answers_formset.total_form_count }};
      $('#add_more_{{ question_form.prefix }}').click(function() {
        var form = '{{answers_formset.empty_form.as_custom|escapejs}}'.replace(/__prefix__/g, form_count_{{ question_form.prefix }});
        $('#answers_div_{{ question_form.prefix }}').append(form);
        CKEDITOR.replace("id_questionanswers_" + '{{ question_form.prefix }}'.split('_')[1] + "-" + form_count_{{ question_form.prefix }} + "-answer", {"filebrowserWindowWidth": 940, "disableNativeSpellChecker": false, "filebrowserUploadUrl": "/ckeditor/upload/", "extraPlugins": "equation", "height": 80, "width": 640, "resize_enabled": false, "removePlugins": "scayt,menubutton,contextmenu,elementspath", "filebrowserBrowseUrl": "/ckeditor/browse/", "skin": "django", "filebrowserWindowHeight": 725, "toolbar": [["Bold", "Italic", "Underline", "Subscript", "Superscript", "-", "Image", "Link", "Unlink", "SpecialChar", "equation", "-", "Format", "-", "Maximize", "-", "Table", "-", "BulletedList", "NumberedList", "-", "PasteText", "PasteFromWord"]]});
        form_count_{{ question_form.prefix }}++;
        $('#id_{{ answers_formset.prefix }}-TOTAL_FORMS').val(form_count_{{ question_form.prefix }});
      });
    
      {% if new %}
        $('#id_question_{{ new }}-type').change(function() {
          check_type('{{ new }}');
        });
        $('#id_question_{{ new }}-point_value').change(function() {
            check_correct_answer("{{ new }}");
        });
        $('input')
            .filter(function() {
                return this.id.match(/id_questionanswers_{{ new }}-.*-point_value/);
            })
            .change(function() {
                check_correct_answer("{{ new }}");
            })
        ;
        check_correct_answer("{{ new }}");
      {% else %}
        $('#id_question_' + {{ question.id }} + '-type').change(function() {
          check_type({{ question.id }});
        });
        $('#id_question_{{ question.id }}-point_value').change(function() {
            check_correct_answer("{{ question.id }}");
        });
        $('input')
            .filter(function() {
                return this.id.match(/id_questionanswers_{{ question.id }}-.*-point_value/);
            })
            .change(function() {
                check_correct_answer("{{ question.id }}");
            })
        ;
        check_correct_answer("{{ question.id }}");
      {% endif %}
    </script>
  </form>
{% if new %}
    </div>
{% endif %}